$(function() {
	report.form.init();
});

Namespace('report.form',{
	init:function() {
		easyui.util.initDatagrid('#datagrid',{
			onDblClickRow:function() {
				report.form.update();
			}
		});
	},
	query:function() {
		$('#query-form').show();
		$('#query-dialog').dialog({
			title: '查询',
		    width: 350,
		    height: 250,
		    buttons:[{
				text:'查询',
				iconCls:'icon-ok',
				handler:function(){
					report.form.doQuery();
					$('#query-form').form('reset');
				}
			},{
				text:'重置',
				iconCls:'icon-undo',
				handler:function(){
					$('#query-form').form('reset');
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#query-dialog').dialog('close');
				}
			}]
		});
	},
	doQuery:function() {
		if(!$('#query-form').form('validate')) return;
		var data=$('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load',data);
		$('#query-dialog').dialog('close');
	},
	update:function() {
		var row = $('#datagrid').datagrid('getSelected');
		if(row==null) {
			top.showInfo('请选择需要修改的记录!');
			return;
		}
		console.log(row);
		getDialog('update-dialog').dialog({
			href:contextpath+'/report/form/update?formId='+row.id,
			title: '修改',
		    width: 750,
		    height: 500,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					report.form.doUpdate();
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#update-dialog').dialog('close');
				}
			}]
		});
	},
	doUpdate:function() {
		if(!$('#update-form').form('validate')) return;
		var datagrid=$("#updateDatagrid").datagrid();
		var row = $('#datagrid').datagrid('getSelected');
		var data=$('#update-form').form('jsonObject');
		var formId=$("#formId").val();
		var editRow = datagrid.datagrid('getRowIndex');
		console.log(editRow);
		datagrid.datagrid('endEdit', editRow);
		datagrid.datagrid('unselectAll');
		var paramList = $("#updateDatagrid").datagrid('getChanges');
		var columnList=[{}];
		var params={
			id:formId,
			name:data.name,
			hsql:data.hsql,
			useColumn: 0,
			description:data.description,
			paramList:paramList,
			columnList: columnList
		};
		console.log(params);
		$('#update-dialog').dialog('close');
		$('#datagrid').datagrid('loading');
		$.postJson(contextpath+'/report/form/update/'+row.id,params,function(result) {
			$('#datagrid').datagrid('loaded');
			console.log(result);
			if(result.code==200) {
				top.showInfo('修改成功!');
				$('#datagrid').datagrid('reload');
			} else {
				top.showInfo('修改失败!');
			}
		});
	},
	add:function() {
		getDialog('add-dialog').dialog({
			href:contextpath+'/report/form/add',
			title: '添加',
		    width: 750,
		    height: 520,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					report.form.doAdd();
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#add-dialog').dialog('close');
				}
			}]
		});
	},
	doAdd:function() {
		if(!$('#add-form').form('validate')) return;
		var datagrid=$("#addDatagrid").datagrid();
		var data=$('#add-form').form('jsonObject');
		var editRow = datagrid.datagrid('getRowIndex');
		console.log(editRow);
		datagrid.datagrid('endEdit', editRow);
		datagrid.datagrid('unselectAll');
		var paramList = $("#addDatagrid").datagrid('getChanges');
		var columnList = [{}];
		var params={
			name:data.name,
			hsql:data.hsql,
			description:data.description,
			useColumn: 0,
			paramList:paramList,
			columnList:columnList
		};
		console.log(params);
		$('#add-dialog').dialog('close');
		datagrid.datagrid('loading');
		$.postJson(contextpath+'/report/form/add',params,function(result) {
			datagrid.datagrid('loaded');
			console.log(result);
			if(result.code==200) {
				top.showInfo('成功!');
				$('#datagrid').datagrid('reload');
			} else {
				top.showInfo('添加失败!');
			}
		});
	},
	remove:function() {
		var row = $('#datagrid').datagrid('getSelected');
		if(row==null) {
			top.showInfo('请选择需要删除的记录!');
			return;
		}
		
		$.messager.confirm('信息提示','确定要删除吗？',function(sure){
		    if(!sure) return;
		    $('#datagrid').datagrid('loading');
		    $.post(contextpath+'/report/form/delete?formId='+row.formId,function(result) {
				$('#datagrid').datagrid('loaded');
				console.log(result.code);
				if(result.code==200) {
					$('#datagrid').datagrid('reload');
				} else {
					top.showInfo('删除失败!');
				}
			},'json');
		});
	},
	
	print:function(){
		window.print();
	},
	
	exported:function() {
		var row = $('#datagrid').datagrid('getSelected');
		if(row==null) {
			top.showInfo('请选择需要导出的记录!');
			return;
		}
		getDialog('exported-dialog').dialog({
			href:contextpath+'/report/form/params',
			title: '导出',
		    width: 500,
		    height: 320,
		    buttons:[{
				text:'预览',
				iconCls:'icon-ok',
				handler:function() {
					report.form.doExported();
				}
			},{
				text:'下载',
				iconCls:'icon-cancel',
				handler:function(){
					report.form.doDownload();
				}
			}]
		});	

	},
	doExported:function() {
		getDialog('preview-dialog').dialog({
			href:contextpath+'/report/form/preview',
			title: '预览',
		    width: 500,
		    height: 320,
		    buttons:[{
				text:'确定',
				iconCls:'icon-cancel',
				handler:function() {
					$('#preview-dialog').dialog('close');
				}
			}]
		});	
	},
	doDownload: function(){
		var row = $('#datagrid').datagrid('getSelected');
		var datagrid=$("#export-datagrid").datagrid();
		var selectRow=datagrid.datagrid("getSelected");
		var selectRowIndex=datagrid.datagrid('getRowIndex',selectRow);
		datagrid.datagrid("endEdit", selectRowIndex);
		//拿到全部数据
		var data=datagrid.datagrid("getData");
		if(data&&row){
			var newDatas=data.rows;
			var paramsdata=JSON.stringify(newDatas); 
			if (paramsdata){
				var params=paramsdata.substring(1,paramsdata.length-1);
				console.log(params);
				params=encodeURIComponent(params);
				window.open( contextpath+'/report/form/download?formId='+row.id+"&params="+params);
			}
		}
	},
	
	history:function(){
		var row = $('#datagrid').datagrid('getSelected');
		getDialog('history-dialog').dialog({
			href:contextpath+'/report/formHis?formId='+row.id,
			title: '历史SQL',
		    width: 500,
		    height: 320,
		    buttons:[{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#history-dialog').dialog('close');
				}
			}]
		});
	}
});